import Action from './Action'

export default class ImSettlementBill extends Action {
    columns: any
    tableIndexArr: Array<{ name: string; key: string }>

    constructor() {
        super()
        this.tableName = 'im_settlement_bill'

        this.columns = {
            id: 'BIGINT(20) PRIMARY KEY NOT NULL', //消息id（服务器）
            userId: "BIGINT(20) NOT NULL DEFAULT '0'",
            ownershipUserId: "BIGINT(20) DEFAULT '0'",
            merchantId: "BIGINT(20) NOT NULL DEFAULT '0'",
            merchantName: 'VARCHAR(64)',
            no: "BIGINT(20) NOT NULL DEFAULT '0'",
            title: 'VARCHAR(64)',
            content: 'TEXT',
            href: 'VARCHAR(128)',
            isRead: "INT(2) DEFAULT '0'", //阅读状态(0-未读，1-已读)
            createTime: 'datetime DEFAULT NULL',
            updateTime: 'datetime DEFAULT NULL',
            delFlag: "tinyint(1) NOT NULL DEFAULT '0'"
        }

        this.tableIndexArr = [
            { name: 'idx_im_settlement_bill_user_id', key: 'user_id' },
            { name: 'idx_im_settlement_bill_no', key: 'no' }
        ]
    }

    saveOrUpdateSettlementBill(data: any) {
        return this.saveOrUpdate(data)
    }

    historyList(userId: any, firstTime: any) {
        return new Promise((resolve, reject) => {
            let timeCdnStr = '',
                limit = ''
            if (firstTime) {
                timeCdnStr = `update_time < '${firstTime}'`
                limit = 'limit 10'
            }
            this.getList({
                where: `where user_id = ${userId}
                    and ${timeCdnStr} order by update_time desc ${limit}`
            }).then((brecords: any) => {
                if (brecords && brecords.length > 0) {
                    resolve(brecords)
                } else {
                    resolve([])
                }
            })
        })
    }

    getUnreadIdList(userId: string) {
        return new Promise((resolve, reject) => {
            this.getList({
                selects: `id`,
                where: `where user_id = ${userId}
                    and is_read in (0, 2)`
            }).then((brecords: any) => {
                if (brecords && brecords.length > 0) {
                    resolve(brecords.map((it: any) => it.id))
                } else {
                    resolve([])
                }
            })
        })
    }
}
